import numpy as np
import numpy as np
import splines as sp
import integration as integ
import load as ld
import matplotlib.pyplot as mp


def curveLength (integMethod, fprime, x, y, n):
	newf = lambda x: np.sqrt(1+(fprime(x)*fprime(x)))
	return integMethod(newf, x, y, n)

def test():

    a = 0.
    b = 10.
    N = 100
    
    X = ld.ex
    Y = ld.ey
    iX = ld.ix
    iY = ld.iy
    fprime = sp.cubicSplineInterpolationDerivative(X,Y)
    f2prime = sp.cubicSplineInterpolationDerivative(iX,iY)
    #print "RES ? :",curveLength(integ.Imidpoint, fprime, 0., 10., 1)
    #print "RES2 ? :",curveLength(integ.Isimpson, f2prime, 0., 10., 1)
    
    c = range(0,N)
    
    x = range(0,N)
    y = range(0,N)
    z = range(0,N)
    t = range(0,N)
    
    for i in range(1,N):
        x[i] = curveLength(integ.Irectg,fprime, a, b, i)
        y[i] = curveLength(integ.Itrap,fprime, a, b, i)
        z[i] = curveLength(integ.Imidpoint,fprime, a, b, i)
        t[i] = curveLength(integ.Isimpson,fprime, a, b, i)
    
    mp.clf()
    mp.semilogx()
    plotRect, = mp.plot(c, x, linewidth=1.0)
    plotTrap, = mp.plot(c, y, linewidth=1.0)
    plotMilieu, = mp.plot(c, z, linewidth=1.0)
    plotSimpson, = mp.plot(c, t, linewidth=1.0)
    
    mp.xlabel("N max")
    mp.ylabel("Longueur de courbe")
    mp.title("Tests longueur de courbe exterieure")
    mp.legend([plotRect,plotTrap,plotMilieu,plotSimpson],["Rectangle","Trapeze","Milieu","Simpson"])
    mp.savefig("../img/longueur exterieure")
    mp.show()

    for i in range(1,N):
        x[i] = curveLength(integ.Irectg,f2prime, a, b, i)
        y[i] = curveLength(integ.Itrap,f2prime, a, b, i)
        z[i] = curveLength(integ.Imidpoint,f2prime, a, b, i)
        t[i] = curveLength(integ.Isimpson,f2prime, a, b, i)
    
    mp.clf()
    mp.semilogx()
    plotRect, = mp.plot(c, x, linewidth=1.0)
    plotTrap, = mp.plot(c, y, linewidth=1.0)
    plotMilieu, = mp.plot(c, z, linewidth=1.0)
    plotSimpson, = mp.plot(c, t, linewidth=1.0)
    
    mp.xlabel("N max")
    mp.ylabel("Longueur de courbe")
    mp.title("Tests longueur de courbe interieure")
    mp.legend([plotRect,plotTrap,plotMilieu,plotSimpson],["Rectangle","Trapeze","Milieu","Simpson"])
    mp.savefig("../img/longueur interieure")
    mp.show()
